package class07;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @description: https://leetcode.com/problems/binary-tree-level-order-traversal-ii
 * @time: 2021/7/17 8:45
 * @author: lhy
 */
public class BinaryTreeLevelOrderTraversal {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> list = new LinkedList<>();
        if (root == null){
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            List<Integer> curAns = new LinkedList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.poll();
                assert curNode != null;
                curAns.add(curNode.val);
                if (curNode.left != null){
                    queue.add(curNode.left);
                }
                if (curNode.right != null){
                    queue.add(curNode.right);
                }
            }
            list.add(0,curAns);
        }
        return list;
    }
}
